﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utilities;

namespace Utilities
{
    public class Paging
    {
        public int currentPage { get; set; }

        public int pageSize { get; set; }

        public long total { get; set; } = 0;

        public static Paging Instance()
        {
            Paging paging = new Paging();
            paging.currentPage = 1;
            paging.pageSize = 10;
            return paging;
        }

        public static Paging Instance(Paging paging)
        {
            Paging _paging = new Paging();
            _paging.currentPage = paging.currentPage;
            _paging.pageSize = paging.pageSize;
            return _paging;
        }
        public Paging Copy()
        {
            Paging paging = new Paging
            {
                currentPage = currentPage,
                pageSize = pageSize
            };
            return paging;
        }

        public Paging setCurrentPage(int currentPage)
        {
            this.currentPage = currentPage;
            return this;
        }

        public Paging setPageSize(int pageSize)
        {
            this.pageSize = pageSize;
            return this;
        }

        public Paging setTotal(int total)
        {
            this.total = total;
            return this;
        }

        public static Paging Analysis(dynamic pageObj)
        {
            Paging paging = Instance();
            if (pageObj != null)
            {
                if (DynamicHelper.IsPropertyExist(pageObj, "page"))
                {
                    dynamic _page = pageObj.page;

                    if (DynamicHelper.IsPropertyExist(_page, "page"))
                    {
                        int page = (int)_page.page;
                        if (page >= 1) paging.currentPage = page;
                    }

                    if (DynamicHelper.IsPropertyExist(_page, "pagesize"))
                    {
                        int pagesize = (int)_page.pagesize;
                        if (pagesize >= 1) paging.pageSize = pagesize;
                    }
                    else if (DynamicHelper.IsPropertyExist(_page, "pageSize"))
                    {
                        int pageSize = (int)_page.pageSize;
                        if (pageSize >= 1) paging.pageSize = pageSize;
                    }
                }
                else if (DynamicHelper.IsPropertyExist(pageObj, "currentPage"))
                {
                    int currentPage = (int)pageObj.currentPage;
                    if (currentPage >= 1) paging.currentPage = currentPage;

                    if (DynamicHelper.IsPropertyExist(pageObj, "pagesize"))
                    {
                        int pagesize = (int)pageObj.pagesize;
                        if (pagesize >= 1) paging.pageSize = pagesize;
                    }
                    else if (DynamicHelper.IsPropertyExist(pageObj, "pageSize"))
                    {
                        int pageSize = (int)pageObj.pageSize;
                        if (pageSize >= 1) paging.pageSize = pageSize;
                    }
                }
                else if (DynamicHelper.IsPropertyExist(pageObj, "pagesize"))
                {
                    int pagesize = (int)pageObj.pagesize;
                    if (pagesize >= 1) paging.pageSize = pagesize;
                }
                else if (DynamicHelper.IsPropertyExist(pageObj, "pageSize"))
                {
                    int pageSize = (int)pageObj.pageSize;
                    if (pageSize >= 1) paging.pageSize = pageSize;
                }
            }
            return paging;
        }
    }
}